package com.cgc.service.impl;


import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.cgc.dao.IMenuDao;
import com.cgc.entity.Menu;
import com.cgc.service.MenuService;

@Service
public class MenuServiceImpl extends BaseServiceImpl<Menu> implements MenuService {

	public IMenuDao menuDao;

	public IMenuDao getMenuDao() {
		return menuDao;
	}

	@Autowired
	public void setMenuDao(IMenuDao menuDao) {
		this.menuDao = menuDao;
		super.setBasedao(this.menuDao);
	}

	@Override
	public Menu getMenuList(Menu menu) {
		List<Menu> list =menuDao.getList(menu);
		
		Menu rmenu = new Menu();
		
		//拿 到父节点的list
		
		List<Menu> parentlist = new ArrayList<Menu>();
		for(Menu m:list){
			if(m.getP_id()==0){
				rmenu.setMenuid(m.getMenuid());
				rmenu.setMenuname(m.getMenuname());
				rmenu.setIcon(m.getIcon());
				rmenu.setUrl(m.getUrl());
				rmenu.setP_id(m.getP_id());
				parentlist.add(m);
			}
		}
		list.removeAll(parentlist); //删除父节点
		getChirend(parentlist,list);
	    if(parentlist.size()>0){
	    	Menu pmenu =parentlist.get(0);
	    	rmenu.setMenus(pmenu.getMenus());
	    }
		return rmenu;
	}
	
	
	public void getChirend(List<Menu> parentlist ,List<Menu> chirendlist){
		
		try {
			List<Menu> templist = new ArrayList<Menu>();
			if(chirendlist.size()==0){
				return ;
			}
			for(Menu pmenu: parentlist){
				Integer pid =pmenu.getMenuid();
				for(Menu cmenu:chirendlist){
					Integer cid = cmenu.getP_id();
					if(pid==cid){
						pmenu.getMenus().add(cmenu);
						templist.add(cmenu);
					}
				}
			}
			chirendlist.removeAll(templist);
			getChirend(templist,chirendlist);
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		
	}
	
	
	
}
